package com.dawn.module.hospital.controller.admin.msgsenduser;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.object.BeanUtils;
import static com.dawn.framework.common.pojo.CommonResult.success;

import com.dawn.framework.excel.core.util.ExcelUtils;

import com.dawn.framework.apilog.core.annotation.ApiAccessLog;
import static com.dawn.framework.apilog.core.enums.OperateTypeEnum.*;

import com.dawn.module.hospital.controller.admin.msgsenduser.vo.*;
import com.dawn.module.hospital.dal.dataobject.msgsenduser.MsgSendUserDO;
import com.dawn.module.hospital.service.msgsenduser.MsgSendUserService;

@Tag(name = "管理后台 - 消息推送用户")
@RestController
@RequestMapping("/hospital/msg-send-user")
@Validated
public class MsgSendUserController {

    @Resource
    private MsgSendUserService msgSendUserService;

    @PostMapping("/create")
    @Operation(summary = "创建消息推送用户")
    @PreAuthorize("@ss.hasPermission('hospital:msg-send-user:create')")
    public CommonResult<Long> createMsgSendUser(@Valid @RequestBody MsgSendUserSaveReqVO createReqVO) {
        return success(msgSendUserService.createMsgSendUser(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新消息推送用户")
    @PreAuthorize("@ss.hasPermission('hospital:msg-send-user:update')")
    public CommonResult<Boolean> updateMsgSendUser(@Valid @RequestBody MsgSendUserSaveReqVO updateReqVO) {
        msgSendUserService.updateMsgSendUser(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除消息推送用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hospital:msg-send-user:delete')")
    public CommonResult<Boolean> deleteMsgSendUser(@RequestParam("id") Long id) {
        msgSendUserService.deleteMsgSendUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得消息推送用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hospital:msg-send-user:query')")
    public CommonResult<MsgSendUserRespVO> getMsgSendUser(@RequestParam("id") Long id) {
        MsgSendUserDO msgSendUser = msgSendUserService.getMsgSendUser(id);
        return success(BeanUtils.toBean(msgSendUser, MsgSendUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得消息推送用户分页")
    @PreAuthorize("@ss.hasPermission('hospital:msg-send-user:query')")
    public CommonResult<PageResult<MsgSendUserRespVO>> getMsgSendUserPage(@Valid MsgSendUserPageReqVO pageReqVO) {
        PageResult<MsgSendUserDO> pageResult = msgSendUserService.getMsgSendUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MsgSendUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出消息推送用户 Excel")
    @PreAuthorize("@ss.hasPermission('hospital:msg-send-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMsgSendUserExcel(@Valid MsgSendUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MsgSendUserDO> list = msgSendUserService.getMsgSendUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "消息推送用户.xls", "数据", MsgSendUserRespVO.class,
                        BeanUtils.toBean(list, MsgSendUserRespVO.class));
    }

}